<script setup>
  import { loginByPassWordApi } from '@/services/user.js'
  import { ref } from 'vue'
  import { useUserStore } from '@/store/user.js'
  const user = useUserStore()
  const formData = ref({
    mobile: '13230000019',
    password: 'abc12345'
  })
  const isChecked = ref(false)
  const formRef = ref(null)
  const formRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请填写手机号码' },
        { pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' }
      ]
    },
    password: {
      rules: [
        { required: true, errorMessage: '请输入密码' }
        // { pattern: '^\\d{6}$', errorMessage: '密码格式不正确' }
      ]
    }
  }
  const login = async () => {
    await formRef.value.validate()
    if (!isChecked.value) return uni.utils.toast('未同意协议')
    const res = await loginByPassWordApi(formData.value)
    user.$patch({
      token: res.data.data.token,
      refreshToken: res.data.data.refreshToken
    })
  }
</script>
<template>
  <uni-forms
    class="login-form"
    ref="formRef"
    :model="formData"
    :rules="formRules"
  >
    <uni-forms-item name="mobile">
      <uni-easyinput
        v-model="formData.mobile"
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="password">
      <uni-easyinput
        v-model="formData.password"
        type="password"
        placeholder="请输入密码"
        :input-border="false"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <view class="agreement" @click="isChecked = !isChecked">
      <radio
        :checked="isChecked"
        color="#16C2A3"
        @click.stop="isChecked = !isChecked"
      />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="login">登 录</button>
    <navigator hover-class="none" class="uni-navigator" url=" ">
      忘记密码？
    </navigator>
  </uni-forms>
</template>

<script>
  export default {
    options: {
      styleIsolation: 'shared'
    }
  }
</script>

<style lang="scss">
  @import './styles.scss';
</style>
